package com.fjwt.gz.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.components.oss.service.IOssService;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.DBOSSConfig;
import com.fjwt.gz.core.utils.SpringBeansUtil;
import com.fjwt.gz.service.impl.SysConfigService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OSSUtils {

    @Autowired
    private static SysConfigService sysConfigService;

    /**
     * 上传文件到oss（只上传不下载）
     */
    public static String uploadToOss(File file) {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = dataFormat.format(new Date());
        DBOSSConfig ossConfig = sysConfigService.getOssConfig();
        IOssService iOssService = SpringBeansUtil.getBean(ossConfig.getOssUseType() + "OssService", IOssService.class);
        IOssService.UploadFormParams uploadFormParams = iOssService.genFormParams(OssFileConfig.BIZ_TYPE.UPLOAD, OssFileConfig.getOssFileConfigByBizType(OssFileConfig.BIZ_TYPE.UPLOAD), OssFileConfig.BIZ_TYPE.UPLOAD + "/" + format + ".xlsx");
        try {
            HttpResponse httpResponse = HttpRequest.post(uploadFormParams.getFormActionUrl())
                    .header("Content-Type", "multipart/form-data")
                    .timeout(20000)
                    .form("signature", uploadFormParams.getFormParams().getString("signature"))
                    .form("x-obs-acl", uploadFormParams.getFormParams().getString("x-obs-acl"))
                    .form("content-type", uploadFormParams.getFormParams().getString("content-type"))
                    .form("key", uploadFormParams.getFormParams().getString("key"))
                    .form("policy", uploadFormParams.getFormParams().getString("policy"))
                    .form("accesskeyid", uploadFormParams.getFormParams().getString("accesskeyid"))
                    .form("OSSAccessKeyId", uploadFormParams.getFormParams().getString("OSSAccessKeyId"))
                    .form("file", file)
                    .execute();
            if (ObjectUtils.isEmpty(httpResponse) || 204 != httpResponse.getStatus()) {
                throw new BizException("文件上传oss失败");
            }
        } catch (Exception e) {
            throw new BizException("文件上传oss失败");
        }finally {
            cn.hutool.core.io.FileUtil.del(file);
        }
        return uploadFormParams.getOssFileUrl();
    }

    /**
     * 上传文件到oss（需要下载上传的文件）
     */
    public static String downloadToOss(File file) {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = dataFormat.format(new Date());
        DBOSSConfig ossConfig = sysConfigService.getOssConfig();
        IOssService iOssService = SpringBeansUtil.getBean(ossConfig.getOssUseType() + "OssService", IOssService.class);
        IOssService.UploadFormParams uploadFormParams = iOssService.genFormParams(OssFileConfig.BIZ_TYPE.DOWNLOAD, OssFileConfig.getOssFileConfigByBizType(OssFileConfig.BIZ_TYPE.DOWNLOAD), OssFileConfig.BIZ_TYPE.DOWNLOAD + "/" + format + ".xlsx");
        try {
            HttpResponse httpResponse = HttpRequest.post(uploadFormParams.getFormActionUrl())
                    .header("Content-Type", "multipart/form-data")
                    .timeout(20000)
                    .form("signature", uploadFormParams.getFormParams().getString("signature"))
                    .form("x-obs-acl", uploadFormParams.getFormParams().getString("x-obs-acl"))
                    .form("content-type", uploadFormParams.getFormParams().getString("content-type"))
                    .form("key", uploadFormParams.getFormParams().getString("key"))
                    .form("policy", uploadFormParams.getFormParams().getString("policy"))
                    .form("accesskeyid", uploadFormParams.getFormParams().getString("accesskeyid"))
                    .form("OSSAccessKeyId", uploadFormParams.getFormParams().getString("OSSAccessKeyId"))
                    .form("file", file)
                    .execute();
            if (ObjectUtils.isEmpty(httpResponse) || 204 != httpResponse.getStatus()) {
                throw new BizException("文件上传oss失败");
            }
        } catch (Exception e) {
            throw new BizException("文件上传oss失败");
        }finally {
            cn.hutool.core.io.FileUtil.del(file);
        }
        return uploadFormParams.getOssFileUrl();
    }

}
